app.controller('RegionIpLgCtrl', ['$scope','$state', '$stateParams','$http','DiagramService','$anchorScroll','$location','$filter','DataService',RegionIpLgCtrl]);

function RegionIpLgCtrl($scope,$state, $stateParams,$http,DiagramService,$anchorScroll,$location,$filter,DataService){
	$anchorScroll.yOffset = -40; 
	$location.hash();
	$anchorScroll();

    //判断是否是修改状态
    $scope.changeFlag = '';

    $scope.searchText = ''

    $scope.sortText = 'lgbm'

    $scope.hintOption = {
        show:false,
        hinttitle:'',
        hintinfo:'111',
        typeImg:'../img/对号.png',
        btn:'继续添加',
        cancelBtn:'返回首页'
    }

    $scope.fresh = function(){
        $scope.form={
            lgmc:'',
            type:'旅馆',
            dz:'',
            street:'',
            community:'',
            fzr:'',
            lxdh:'',
            mark:'',
            lgbm:''
        }
        $scope.changeFlag = ''
    }
    $scope.fresh();

    $scope.sfGroup = ['是','否']

    $scope.dateOptions = {
	    changeYear: true,
	    changeMonth: true
    };

    function getCommunity(){
        DataService.restGetApi('communities')
        .then(function success (data) {
            $scope.SAndCGroup = data;
            })
    }

    getCommunity();

    var list = []
    var list2 = []
    var list3 = []     

    function getList(){
        DataService.restGetApi('hotels')
        .then(function success (data) {
            list = data;
            list2 = deepCopy(list)
            list3 = deepCopy(list)
            $scope.getData([1,$scope.pageOption.pageSize])
        })
    }
    getList();

    $scope.pushStreet = function(item){
        if(item!=$scope.form.street){
            $scope.form.street = item;
        }
    }

    $scope.checkCommunity = function(item){
        if($scope.form.community==''||$scope.form.community.street!=item.street){
            $scope.form.community = item;
        }
    }

    $scope.submitForm = function(){
        $scope.importForm.lgmc.$dirty = true;
        $scope.importForm.dz.$dirty = true;
        $scope.importForm.street.$dirty = true;
        $scope.importForm.community.$dirty = true;
        $scope.importForm.lgbm.$dirty = true;
        if($scope.importForm.lgmc.$valid&&$scope.importForm.dz.$valid&&$scope.importForm.street.$valid&&$scope.importForm.community.$valid&&$scope.importForm.lgbm.$valid){
            $scope.form.street = $scope.form.street.street;
            $scope.form.community = $scope.form.community.community;
            DataService.restPostApi('hotels/dateChangeByLgdm',$scope.form)
            .then(function success (data) {
                $scope.hintOption = {
                    show:true,
                    hinttitle:data.top,
                    hintinfo:'',
                    typeImg:'../img/对号.png',
                    btn:'继续添加',
                    cancelBtn:'返回首页'
                }
                $scope.clickCancel = function(){
                    $state.go("app.region.main")
                    $scope.hintOption.show = false;
                }

                $scope.clickSubmit = function(){
                    $scope.fresh();
                    getList();
                    $scope.importForm.lgmc.$dirty = false;
                    $scope.importForm.dz.$dirty = false;
                    $scope.importForm.street.$dirty = false;
                    $scope.importForm.community.$dirty = false;
                    $scope.importForm.lgbm.$dirty = false;
                    $scope.hintOption.show = false;
                    $scope.changeFlag = '';
                }
            })
        }
    }

    $scope.clearInput = function(input){
        $scope.form[input] = ''
    }

    $scope.pageOption = {
	    pageSize:10,
	    itemCount:3 
  	}

	
	$scope.getData = function(item){
		if(item) {
			generateData(item);
		}
  	
	}

    $scope.$watch('searchText',function(newValue,oldValue){
        if(newValue != oldValue){
            var filtList = $filter('filter')(list3,newValue)
            list = $filter('orderBy')(filtList,$scope.sortText)
            list2 = deepCopy(list)
            $scope.pageOption['current'] = 0;
            $scope.getData([1,$scope.pageOption.pageSize])
        }
    })

    $scope.$watch('sortText',function(newValue,oldValue){
        if(newValue != oldValue){
            var filtList = $filter('filter')(list3,$scope.searchText)
            list = $filter('orderBy')(filtList,newValue)
            list2 = deepCopy(list)
            $scope.pageOption['current'] = 0;
            $scope.getData([1,$scope.pageOption.pageSize])
        }
    })

    $scope.clickSort = function(type){
        console.log(type)
        $scope.sortText = type;
    }

    $scope.importType = 'single'

    $scope.changeImportType = function(type){
        $scope.importType = type;
    }

	function generateData(item) {
		$scope.pageOption.itemCount = list.length;
		$scope.commList = list.slice(item[0]-1,item[1]);
	}

    $scope.change = function(index){
        $anchorScroll();
        $scope.changeFlag = 'change'
        $scope.form= list2[index]
        $scope.form.street = {community:list2[index].community,street:list2[index].street};
        $scope.form.community = {community:list2[index].community,street:list2[index].street};
    }

    $scope.delete = function(lgbm){
        if(confirm('确认删除么？')){
            DataService.restPostApi('hotels/deleteByLgbm',{lgbm:lgbm})
            .then(function success (data) {
                $scope.hintOption = {
                    show:true,
                    hinttitle:data.top,
                    hintinfo:'',
                    typeImg:'../img/对号.png',
                    btn:'继续操作',
                    cancelBtn:'返回首页'
                }
                $scope.clickCancel = function(){
                    $state.go("app.region.main")
                    $scope.hintOption.show = false;
                }

                $scope.clickSubmit = function(){
                    getList();
                    $scope.hintOption.show = false;
                }
            })
        }    
    }

    //解析csv
    function analysisCSV(str){
        
    }

}

var deepCopy = function(o) {
            if (o instanceof Array) {
                var n = [];
                for (var i = 0; i < o.length; ++i) {
                    n[i] = deepCopy(o[i]);
                }
                return n;

            } else if (o instanceof Object) {
                var n = {}
                for (var i in o) {
                    n[i] = deepCopy(o[i]);
                }
                return n;
            } else {
                return o;
            }
        }  
